<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 우편번호 검색 컨트롤러
 */
class Zipcode extends CI_Controller {
	
	function __construct() {
		parent::__construct();
		
		$this->load->model('member/Zipcode_model', 'model', TRUE);
		$this->load->helper(array('url'));
		$this->load->helper(array('pagenavi'));
	}

	/**
	 * 우편번호 검색
	 */
	function index() {
		log_message('debug', "@@@@ Zipcode::index()");

		// 폼 정보 가져오기
		$search_type  = $this->input->get('search_type');
		$dong         = $this->input->get('dong');
		$city         = $this->input->get('city');
		$street       = $this->input->get('street');
		$current_page = $this->input->get('page');
		$current_page = empty($current_page) ? 1 : $current_page;
		
		if (!empty($search_type)) {
			$page_size  = 15;
			$block_size = 10;

			if ($search_type == 'dong') {
				if (!empty($dong)) {
					$data['zipcode_list'] = $this->model->select_zipcode_list($dong, $current_page, $page_size);
					$total_cnt = $this->model->get_zipcode_total_count($dong);
					$data['zipcode_total_count'] = $total_cnt;
					$uri = $this->uri->uri_string()."?search_type=$search_type&dong=$dong";
				}
			} elseif ($search_type == 'street') {
				if (!empty($street)) {
					$data['zipcode_street_list'] = $this->model->select_zipcode_street_list($city, $street, $current_page, $page_size);
					$total_cnt = $this->model->get_zipcode_street_total_count($city, $street);
					$data['zipcode_street_total_count'] = $total_cnt;
					$uri = $this->uri->uri_string()."?search_type=$search_type&city=$city&street=$street";
				}
			}

			$data['navi'] = page_navigation($current_page, $total_cnt, $page_size, $block_size, $uri);
		}

		$data['search_type'] = empty($search_type) ? '' : $search_type;
		$data['page']        = $current_page;
		$data['dong']        = empty($dong) ? '' : $dong;
		$data['city']        = empty($city) ? '' : $city;
		$data['street']      = empty($street) ? '' : $street;

		$this->load->view('member/zipcode_pop_v', $data);

	}
	
	/**
	 * 우편번호 검색 결과를 JSON으로 리턴
	 */
	function json() {
		log_message('debug', "@@@@ Zipcode::json2()");
		
		header("Content-Type: text/javascript; charset=utf-8");
		header("Access-Control-Allow-Origin: *");
		header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
		header("Access-Control-Allow-Headers:origin, x-requested-with");


		// 폼 정보 가져오기
		$search_type  = $this->input->get('search_type');
		$dong         = $this->input->get('dong');
		$city         = $this->input->get('city');
		$street       = $this->input->get('street');
		$current_page = $this->input->get('page');
		$current_page = empty($current_page) ? 1 : $current_page;

		echo isset($_GET["callback"]) ? $_GET["callback"] : "alert";
		
		// 검색어가 없을경우
		if (empty($search_type) || (empty($dong) && empty($street) )) {
			echo '({"results":"FAIL"})';
			exit();
		}

		echo '(';
		echo '{';

		$page_size  = 15;
		$block_size = 10;

		if ($search_type == 'dong') {
			if (!empty($dong)) {
				$zipcode_list = $this->model->select_zipcode_list($dong, $current_page, $page_size);
				$total_cnt = $this->model->get_zipcode_total_count($dong);
				$uri = $this->uri->uri_string()."?search_type=$search_type&dong=$dong";
			}
		} elseif ($search_type == 'street') {
			if (!empty($street)) {
				$zipcode_list = $this->model->select_zipcode_street_list($city, $street, $current_page, $page_size);
				$total_cnt = $this->model->get_zipcode_street_total_count($city, $street);
				$uri = $this->uri->uri_string()."?search_type=$search_type&city=$city&street=$street";
			}
		}
		$navi = page_navigation($current_page, $total_cnt, $page_size, $block_size, $uri);
		
		echo '"search_type":"'.$search_type.'",';
		echo '"total_cnt":"'.$total_cnt.'",';
		echo '"zipcode_list":[';
		$cnt = count($zipcode_list);
		for ($i=0; $i < $cnt ; $i++) {
			$item = $zipcode_list[$i];
			echo '{';
			echo '"zipcode":"'.$item['zipcode'].'",';
			echo '"address_dong":"'.($search_type == 'dong' ? '' : trim($item['address_dong'])).'",';
			echo '"address_full":"'.trim($item['address_full']).'",';
			echo '"addr":"'.$item['addr'].'"';
			echo '}';
			if($i < $cnt-1) {
				echo ',';
			}
		}

		echo '],';
		echo '"navi":{';
		echo '"current_page":"'.$navi['current_page'].'",';
//		echo '"prev_page_link":"'.$navi['prev_page_link'].'",';
//		echo '"next_page_link":"'.$navi['next_page_link'].'",';
		echo '"page_count":"'.$navi['page_count'].'",';
//		echo '"page_count_link":"'.$navi['page_count_link'].'",';
		echo '"jump_prev_page":"'.$navi['jump_prev_page'].'",';
		echo '"jump_next_page":"'.$navi['jump_next_page'].'",';
		echo '"page_nums":[';
		$cnt = count($navi['page_nums']);
		for ($i=0; $i < $cnt ; $i++) {
			$item = $navi['page_nums'][$i];
			echo '{';
			echo '"page_no":"'.$item['page_no'].'"';
//			echo '"url":"'.$item['url'].'"';
			echo '}';
			if($i < $cnt-1) {
				echo ',';
			}
		}
		echo ']}}';
		echo ')';
		exit();
	}
}

/* End of file zipcode.php */
/* Location: ./application/controllers/member/zipcode.php */